
include('physicalentity');
include('graphics.spritenode');
include('events');

var SHIP_SPRITE_URL = 'assets/ship.png';

// A spaceship within the game world.
new_class(
  'SpaceShip',
  function() {
    PhysicalEntity.call(this, 0, 0);
    this.name = 'Roger';
    this.addChild(new graphics.SpriteNode(SHIP_SPRITE_URL));
    this.ticks = 0;
    this.dying = false;
  },
  {
    update: function(dt) {
      //console.log(this.super)
      this.super.update.call(this, dt);
      this.x = this.body.getX();
      this.y = this.body.getY();
      this.angle = this.body.getAngle();

      if (this.dying) {
        this.ticks += 1;
        this.scaleX = this.scaleY = this.scaleX - 0.025;
        // TODO(stevet): Replace this scale hack with a death animation
        // duration.
        if (this.scaleX < 0)
          this.scaleX = 0;
        if (this.scaleX == 0) {
          console.log('Ship destroyed...');
          this.notify('shipDestroyed');
          this.parentNode.removeChild(this);
        }
      }
    },
    destroy: function() {
      if (this.dying) return;
      this.dying = true;
    },
    shape: function() {
      return new physics.shapes.Circle(this.x, this.y, 10);
    }
  },
  PhysicalEntity);

add_events_to_class(SpaceShip);
